package week12.chat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.util.ArrayList;

/**
 * @author The setting sun
 * @version 1.0
 * @title: LoginUI
 * @description: 登录界面
 * @date 2021/11/25 10:52
 */
public class LoginUI {
    public static void main(String[] args) {
        new LoginUI().initLogin();
    }

    JFrame frame = new JFrame("登录");

    JPanel loginPanelMax = new JPanel();

    JPanel loginPanelMin1 = new JPanel();
    JLabel accountLabel = new JLabel("账号：");
    JTextField accountText = new JTextField(22);

    JPanel loginPanelMin2 = new JPanel();
    JLabel pwdLabel = new JLabel("密码：");
    JTextField pwdText = new JTextField(22);

    JPanel loginPanel3 = new JPanel();
    JLabel hint = new JLabel("如果你有账号请点击登录，没有请点击注册");

    JPanel loginPanel4 = new JPanel();
    JButton loginButton = new JButton("登录");
    JButton registerButton = new JButton("注册");

    public void initLogin() {
        accountText.setText("长度请小于14个字符");
        pwdText.setText("长度请小于14个字符");
        accountLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        pwdLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        hint.setFont(new Font("微软雅黑", Font.BOLD, 18));

        accountText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                accountText.setText("");
            }
        });

        pwdText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                pwdText.setText("");
            }
        });

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection connection = JDBCOperation.getConnection();

                String sql = "select * from userinfo";
                ArrayList<Account> accounts = JDBCOperation.verificationUser(connection, sql);


                for (int i = 0; i < accounts.size(); i++) {
                    if (accountText.getText().equals(accounts.get(i).getAccount()) && pwdText.getText().equals(accounts.get(i).getPassword())) {
                        frame.setVisible(false);
                        new Client().init();
                    } else {
                        hint.setText("账号或密码错误，请重新输入！");
                    }
                }

                JDBCOperation.closeResource(connection, null, null);
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection connection = JDBCOperation.getConnection();

                String select = "select * from userinfo";
                ArrayList<Account> accounts = JDBCOperation.verificationUser(connection, select);

                int equalsCount = 0;
                for (int i = 0; i < accounts.size(); i++) {
                    if (accountText.getText().equals(accounts.get(i).getAccount())) {
                        equalsCount++;
                        hint.setText("账号已经存在，请重新输入！");
                    }
                }

                if (equalsCount == 0) {
                    String account = accountText.getText();
                    String pwd = pwdText.getText();

                    String update = "insert into userinfo(account, password) values(?, ?)";
                    JDBCOperation.addUser(connection, update, account, pwd);

                    hint.setText("账号创建成功，请点击登录");
                }

//                for (int i = 0; i < accounts.size(); i++) {
//                    System.out.println(accountText.getText());
//                    System.out.println(accounts.get(i).getAccount());
//                    System.out.println(accountText.getText().equals(accounts.get(i).getAccount()));
//                }

                JDBCOperation.closeResource(connection, null, null);

            }
        });

        loginPanelMin1.add(accountLabel);
        loginPanelMin1.add(accountText);

        loginPanelMin2.add(pwdLabel);
        loginPanelMin2.add(pwdText);

        loginPanel3.add(hint);

        loginPanel4.add(loginButton);
        loginPanel4.add(registerButton);

        loginPanelMax.add(loginPanelMin1);
        loginPanelMax.add(loginPanelMin2);
        loginPanelMax.add(loginPanel3, BorderLayout.SOUTH);

        frame.setLayout(new GridLayout(2, 1));
        frame.add(loginPanelMax, BorderLayout.CENTER);
        frame.add(loginPanel4);

        frame.setVisible(true);
        frame.setResizable(false);
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
